package org.lex.notepad;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.UIManager;


import org.lex.app.Application;
import org.lex.notepad.ui.DropHandler;
import org.lex.notepad.ui.MainMenuBar;
import org.lex.notepad.ui.NotepadActions;
import org.lex.notepad.ui.NotepadContenetPane;
import org.lex.notepad.ui.NotepadEditor;
import org.lex.notepad.ui.status.StatusBar;
import org.lex.notepad.util.CK;
import org.lex.swing.Swings;
import org.lex.utils.Codec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Notepad extends Application {
	public static void main(String[] args) {
		log.info("Application startup");
		Notepad.doPrestartInitialize();
		Notepad.launch(Notepad.class, args);
	}

	public static Notepad getApplication() {
		return Application.getApplication(Notepad.class);
	}

	public static final String DEFAULT_TITLE = "Notepad";
	private static final Logger log = LoggerFactory.getLogger(Notepad.class);

	private JFrame mainFrame = null;
	private NotepadContenetPane contentPane = null;
	private NotepadActions operations = null;

	@Override
	public String getApplicationID() {
		return "notepad";
	}

	@Override
	public JFrame getRootWindow() {
		if (null == mainFrame) {
			mainFrame = new JFrame();
			mainFrame.setName(this.getClass().getCanonicalName() + ".mainframe");
			mainFrame.setIconImage(new ImageIcon(getClass().getResource("resources/x.png")).getImage());
			mainFrame.setTitle(DEFAULT_TITLE);
			mainFrame.setContentPane(this.getContentPane());
			mainFrame.setJMenuBar(new MainMenuBar());
			mainFrame.addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent e) {
					getNotepadActions().exitAction();
				}
			});
		}
		return mainFrame;
	}

	public NotepadActions getNotepadActions() {
		if (null == operations)
			operations = new NotepadActions();
		return operations;
	}

	public NotepadEditor getEditor() {
		return this.getContentPane().getEditor();
	}

	public StatusBar getStatusBar() {
		return this.getContentPane().getStatusBar();
	}

	@Override
	protected void initialize(String[] args) {
		DropHandler.registerDropHandler(this.getRootWindow());
		this.getContext().getSessionStorage().restore(this.getRootWindow());
		this.getRootWindow().setVisible(true);
	}

	@Override
	protected void shutdown() {
		this.getContext().getSessionStorage().save(this.getRootWindow());
	}

	private NotepadContenetPane getContentPane() {
		if (null == contentPane)
			contentPane = new NotepadContenetPane();
		return contentPane;
	}

	private static void doPrestartInitialize() {
		log.trace("set swing.aatext to: " + true);
		System.setProperty("swing.aatext", "true");
		log.trace("set swing.boldMetal to: " + false);
		Swings.setUseBoldMetalFont(false);
		setupLookAndFeel();
		setupFont();
	}

	private static void setupLookAndFeel() {
		try {
			String laf = Notepad.getApplication().getContext().getResourceManager().getResourceMap().getString(
					CK.UI_LOOK_AND_FEEL);
			if (null != laf && false == laf.isEmpty()) {
				log.debug("set swing laf to: " + laf);
				UIManager.setLookAndFeel(laf);
			}
		} catch (Exception e) {
			log.warn("change laf failed", e);
			Swings.tryToUseCrossPlatformLookAndFeel();
		}
	}

	private static void setupFont() {
		// FontCache cache = new FontCache();
		String str = Notepad.getApplication().getContext().getResourceManager().getResourceMap().getString(CK.UI_FONT, null);
		if (null != str && false == str.isEmpty())
			Swings.setDefaultFont(Codec.decodeFont(str));
	}
}
